package com.xudow.app.ui.adapter.qanda;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.qanda.Question;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.model.qanda.P2PMessage;
import com.xudow.app.util.ImageUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 单独提问Adapter
 *
 * @author lukecheng
 */
public class P2PQuestionAdapter extends ArrayAdapter<P2PMessage> {

    private LayoutInflater inflater;

    public P2PQuestionAdapter(Context context, int resource, List<P2PMessage> objects) {
        super(context, resource, objects);

        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        P2PMessage question = getItem(position);

        ViewHolder viewHolder;

        if (null == convertView) {
            if (question.isExpert) {
                convertView = inflater.inflate(R.layout.item_p2p_expert_message_list, null);
            } else {
                convertView = inflater.inflate(R.layout.item_p2p_message_list, null);
            }

            viewHolder = new ViewHolder();
            viewHolder.portrait = (CircleImageView) convertView.findViewById(R.id.portrait);
            viewHolder.username = (TextView) convertView.findViewById(R.id.username);
            viewHolder.time = (TextView) convertView.findViewById(R.id.time);
            viewHolder.message = (TextView) convertView.findViewById(R.id.message);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ImageUtils.loadImage(getContext(), viewHolder.portrait, question.portraitUrl, R.mipmap.def_portrait);
        viewHolder.username.setText(question.username);
        viewHolder.time.setText(question.time);
        viewHolder.message.setText(question.message);

        return convertView;
    }

    class ViewHolder {
        public CircleImageView portrait;
        public TextView username;
        public TextView time;
        public TextView message;
    }

}
